package com.tutu.model.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.tutu.model.bean.GroupInfo;
import com.tutu.model.bean.InvitationInfo;
import com.tutu.model.bean.UserInfo;
import com.tutu.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 图图 2020/10/11
 *
 * 邀请信息表操作类
 *
 */
public class InviteTableDao {
    private DBHelper mHelper;

    public InviteTableDao(DBHelper mHelper) {
        this.mHelper = mHelper;
    }

    /**
     * 添加邀请
     * @param invitationInfo
     */
    public  void addInvitation(InvitationInfo invitationInfo){
        SQLiteDatabase database = mHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_REASON,invitationInfo.getReason());
        // ordinal 表示枚举的序号，从0开始
        values.put(InviteTable.COL_STATUS,invitationInfo.getStatus().ordinal());

        UserInfo userInfo = invitationInfo.getUserInfo();
        if(userInfo != null){
            // 是联系人邀请
            values.put(InviteTable.COL_USER_HXID,invitationInfo.getUserInfo().getHxid());
            values.put(InviteTable.COL_USER_NAME,invitationInfo.getUserInfo().getUsername());

        }else{
            // 是群组邀请
            values.put(InviteTable.COL_USER_HXID,invitationInfo.getGroupInfo().getInvatePerson());
            values.put(InviteTable.COL_GROUP_HXID,invitationInfo.getGroupInfo().getGroupId());
            values.put(InviteTable.COL_GROUP_NAME,invitationInfo.getGroupInfo().getGroupName());
        }

        database.replace(InviteTable.TAB_NAME,null,values);

    }

    /**
     * 获取所有邀请信息
     * @return
     */
    public List<InvitationInfo> getInvitationInfos(){
        List<InvitationInfo> invitationInfos = new ArrayList<>();
        SQLiteDatabase database = mHelper.getReadableDatabase();

        String sql = "select * from "+ InviteTable.TAB_NAME;
        Cursor cursor = database.rawQuery(sql, null);

        while (cursor.moveToNext()){
            InvitationInfo info = new InvitationInfo();
            // 邀请原因
            info.setReason(cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON)));
            // 邀请状态
            info.setStatus( int2InvitationStatus(cursor.getInt(cursor.getColumnIndex(InviteTable.COL_STATUS))) );

            String groupId = cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID));
            if(TextUtils.isEmpty(groupId)){
                // 联系人的邀请 信息
                UserInfo userInfo = new UserInfo();
                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));
                userInfo.setUsername(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                userInfo.setNick(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                info.setUserInfo(userInfo);
            }else{
                // 群组的邀请信息
                GroupInfo groupInfo = new GroupInfo();
                groupInfo.setGroupId(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID)));
                groupInfo.setGroupName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_NAME)));
                groupInfo.setInvatePerson(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));
                info.setGroupInfo(groupInfo);
            }

            invitationInfos.add(info);
        }

        cursor.close();

        return invitationInfos;
    }

    /**
     * 将int类型状态转换为邀请状态
     * @return
     */
    public  InvitationInfo.InvitationStatus int2InvitationStatus(int status){
        if(status == InvitationInfo.InvitationStatus.NEW_INVITE.ordinal()){
            return  InvitationInfo.InvitationStatus.NEW_INVITE;
        }
        if(status == InvitationInfo.InvitationStatus.NEW_GROUP_INVITE.ordinal()){
            return  InvitationInfo.InvitationStatus.NEW_GROUP_INVITE;
        }
        if(status == InvitationInfo.InvitationStatus.INVITE_ACCEPT_PEER.ordinal()){
            return  InvitationInfo.InvitationStatus.INVITE_ACCEPT_PEER;
        }
        if(status == InvitationInfo.InvitationStatus.INVITE_ACCEPT.ordinal()){
            return  InvitationInfo.InvitationStatus.INVITE_ACCEPT;
        }
        if(status == InvitationInfo.InvitationStatus.GROUP_REJECT_INVITE.ordinal()){
            return  InvitationInfo.InvitationStatus.GROUP_REJECT_INVITE;
        }
        if(status == InvitationInfo.InvitationStatus.GROUP_REJECT_APPLICATION.ordinal()){
            return  InvitationInfo.InvitationStatus.GROUP_REJECT_APPLICATION;
        }
        if(status == InvitationInfo.InvitationStatus.GROUP_INVITE_DECLINED.ordinal()){
            return  InvitationInfo.InvitationStatus.GROUP_INVITE_DECLINED;
        }
        if(status == InvitationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED.ordinal()){
            return  InvitationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED;
        }
        if(status == InvitationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED.ordinal()){
            return  InvitationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED;
        }
        if(status == InvitationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED.ordinal()){
            return  InvitationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED;
        }
        if(status == InvitationInfo.InvitationStatus.GROUP_ACCEPT_INVITE.ordinal()){
            return  InvitationInfo.InvitationStatus.GROUP_ACCEPT_INVITE;
        }
        if(status == InvitationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION.ordinal()){
            return  InvitationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION;
        }

        return null;
    }

    /**
     * 删除邀请
     * @param hxid
     */
    public void removeInvitation(String hxid){
        if(TextUtils.isEmpty(hxid)) return;
        SQLiteDatabase database = mHelper.getReadableDatabase();
        database.delete(InviteTable.TAB_NAME,InviteTable.COL_USER_HXID + "=?",new String[]{hxid});

    }

    /**
     * 更新邀请状态
     * @param status
     * @param hxid
     */
    public void updateInvitationStatus(InvitationInfo.InvitationStatus status,String hxid){
        if(TextUtils.isEmpty(hxid)) return;
        SQLiteDatabase database = mHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_STATUS,status.ordinal());
        database.update(InviteTable.TAB_NAME,values,InviteTable.COL_USER_HXID + "=?",new String[]{hxid});
    }
}
